package com.test.web.controller;
import cn.hutool.json.JSONUtil;
import com.he.core.ResultT;
import com.he.core.excle.data.ReadData;
import com.he.core.excle.data.WriteData;
import com.he.web.util.Webs;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @author heyanjing
 * date:2020-06-09 2020/6/9:22:54
 */
@Slf4j
@AllArgsConstructor
@RestController
public class ExcelController {
    @PostMapping("/excel/import")
    public ResultT excelImport(@RequestPart MultipartFile file) {
        try {
            List<ReadData> readDataList = Webs.excelImport(file, ReadData.class);
            log.info("{}", JSONUtil.toJsonStr(readDataList));
            return ResultT.success();
        } catch (IOException e) {
            e.printStackTrace();
            return ResultT.error();
        }
    }
    @GetMapping("/excel/export")
    public void excelExport(HttpServletResponse response) {
        List<WriteData> list = new ArrayList<>();
        list.add(new WriteData("aaaaaaaaaaaaaaa", 1.1, new Date(), 1, 1, 1.11));
        list.add(new WriteData("bbbbbbbbbbbbbb", 2.2, new Date(), 2, 2, 2.22));
        Webs.excelExport("测试导出", list, WriteData.class, response);
    }
}
